import arealist from '../../utils/area.js'
import requestUrl from '../../utils/requestUtil'



Page({

  /**
   * 页面的初始数据
   */
  data: {
      list:{
          "address":'',
          "area":'',
          "city": '',
          "phName": '',
          "phoneNo": '',
          "province":'', 
          "frontPhotoUrl":'',
      },
      SSQ:'',
    //显示弹出层
    show:false,
    //地区json
    areaList:arealist,
    //正面全身照
    fullBodyPicture: [],
    uploadImgUrl:'',
    isPromise: false,
  },
  onChange(event) {
    this.setData({
      isPromise: event.detail,
    });
  },
  toPromisePage(){
    wx.navigateTo({
      url: '../promisePage/promisePage',
    })
  },
  //提交
  onFormSubmit(e){
      let userToken = wx.getStorageSync('token')
      let data = this.data.list;
      if (this.data.isPromise == false) {
        return wx.showToast({
          title:'请勾选承诺书',
          icon: 'none',    
        })
      }
      for (let i in data){
           if(data[i] == ''){
               return wx.showToast({
                   title:'农户信息未填写完整！',
                   icon: 'none',    
               })
           }
      }
       
        
      requestUrl.requestUrl({
           url: "/app/login/auth",
           params: data,
           header:{
             token:userToken
           }
       }).then((data) => {
         
         if (data.data.code == 0 || data.data.msg == '已认证') {
          wx.navigateBack();
         }else if(data.data.msg == '登录失效'){
            // wx.showToast({
            //     title:'请重新登录！'
            // })
           wx.navigateTo({
               url:'../login/login'
           })
       }
       }).catch((errorMsg) => {
                   
       })
    
  },
  onToPlanting(e){
    let data = this.data.list;
   let userToken = wx.getStorageSync('token')
     for (let i in data){
         if(data[i] == ''){
             return wx.showToast({
                 title:'农户信息未填写完整！',
                 icon: 'none',    
             })
         }
     }
    requestUrl.requestUrl({
         url: "/app/login/auth",
         params: data,
         header:{
           token:userToken
         }
     }).then((data) => {
       
       if (data.data.code == 0 || data.data.msg == '已认证') {
        wx.navigateTo({
            url:'../register/register?type=2',
        });
       }else if(data.data.msg == '登录失效'){
           wx.navigateTo({
               url:'../login/login'
           })
       }
       
     }).catch((errorMsg) => {
                 
     })
  },
  //显示弹出层
  showPopup(){
    this.setData({
      show:true
    })
  },
  //确定选择省市区
  determine(e){
      var a = e.detail.values;
      var strArr = [];
      a.forEach(item => {
          if(item != undefined){
               strArr.push(item.name);
          }
       
      })
          this.setData({
              SSQ: strArr.join(''),
              [`list.province`]: a[0].code,
              [`list.city`]: a[1].code,
              [`list.area`]: a[2].code,
              show: false
          })
  },
  //取消选择省市区
  cancel(e){
    this.setData({
      show:false
    })
  },
  /**
   * 上传图片
   */
  afterRead(e) {
      let userToken = wx.getStorageSync('token')
    var that = this;
    const { file } = e.detail;
    // 上传完成需要更新 fileList
    const { fullBodyPicture = [] } = this.data;
    fullBodyPicture.push({ ...file, url: e.data });
    this.setData({ fullBodyPicture });
    // uploadImgUrl
    wx.showLoading();
    wx.uploadFile({
      url: 'https://nhwechart.nsb-farmall.com/app/upload',
      filePath: file.url,
      name: 'file',
      header: { 
        "content-Type": "multipart/form-data;charset=utf-8",
        'token':userToken  
      },
       success(res) {
         let Imgurl = JSON.parse(res.data).url;
          that.setData({
           [`list.frontPhotoUrl`]:Imgurl
          })
          wx.hideLoading();
       },
       fail(err) {
         console.log(err);
       }
   })
  },
  /**
   * 删除图片
   */
  deletePic(e) {
    // 上传完成需要更新 fileList
    const { fullBodyPicture = [] } = this.data;
    fullBodyPicture.splice(e.detail.index,1);
    this.setData({ fullBodyPicture });
  },
 
  onLoad: function (options) {

  },

 

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  clickSSQ(){
      this.setData({
          show:true
      })
  },
  onChangeName(event){
      this.setData({
          [`list.phName`]:event.detail
         
      })
        
          
        
  },
  onChangePhone(event){
      this.setData({
          [`list.phoneNo`]:event.detail
         
      })
  },
  onChangeAdressDetail(event){
      this.setData({
          [`list.address`]:event.detail
         
      })
  }

 
})